package com.hqully.labmanage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.labmanage.common.enums.ResultEnum;
import com.hqully.labmanage.common.exception.DAOException;
import com.hqully.labmanage.common.exception.ResultException;
import com.hqully.labmanage.domain.dto.DeviceFaultRecordDTO;
import com.hqully.labmanage.domain.dto.FaultRecordReadDTO;
import com.hqully.labmanage.domain.dto.PageDTO;
import com.hqully.labmanage.domain.entity.DeviceFaultRecord;
import com.hqully.labmanage.domain.entity.FaultRecordRead;
import com.hqully.labmanage.mapper.DeviceFaultRecordMapper;
import com.hqully.labmanage.mapper.FaultRecordReadMapper;
import com.hqully.labmanage.service.DeviceFaultRecordService;
import com.hqully.labmanage.service.impl.constant.MapperConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 针对数据库表[deviceFaultRecord]的操作Service
 * </p>
 *
 * @entity {@link DeviceFaultRecord}
 * @date 2022-10-08 10:05:23 <br>
 * @author hqully <br>
 * @version 1.0
 */
@Slf4j
@Service
public class DeviceFaultRecordServiceImpl implements DeviceFaultRecordService {


    @Resource
    private DeviceFaultRecordMapper deviceFaultRecordMapper;

    @Resource
    private FaultRecordReadMapper recordReadMapper;

    @Override
    public int readFaultRecord(FaultRecordReadDTO faultRecordReadDTO) {
        FaultRecordRead faultRecordRead = new FaultRecordRead();
        BeanUtils.copyProperties(faultRecordReadDTO, faultRecordRead);

        int status;
        try {
            status = recordReadMapper.addFaultRecordRead(faultRecordRead);
        } catch (Exception e) {
            log.error("READ [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", faultRecordReadDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "已读设备故障记录失败");
        }
        return status;
    }

    @Override
    public PageInfo<DeviceFaultRecordDTO> pageDeviceFaultRecords(PageDTO page, DeviceFaultRecordDTO deviceFaultRecordDTO) {
        try {
            PageHelper.startPage(page);
            List<DeviceFaultRecordDTO> deviceFaultRecordList = deviceFaultRecordMapper.listDeviceFaultRecords(deviceFaultRecordDTO);
            return new PageInfo<>(deviceFaultRecordList);
        } catch (Exception e) {
            log.error("PAGE [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceFaultRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备故障记录分页列表失败");
        }
    }

    @Override
    public List<DeviceFaultRecordDTO> listDeviceFaultRecords(DeviceFaultRecordDTO deviceFaultRecordDTO) {
        try {
            return deviceFaultRecordMapper.listDeviceFaultRecords(deviceFaultRecordDTO);
        } catch (Exception e) {
            log.error("LIST [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceFaultRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "查询设备故障记录列表失败");
        }
    }

    @Override
    public int addDeviceFaultRecord(DeviceFaultRecordDTO deviceFaultRecordDTO) {
        DeviceFaultRecord deviceFaultRecord = new DeviceFaultRecord();
        BeanUtils.copyProperties(deviceFaultRecordDTO, deviceFaultRecord);

        int status;
        try {
            status = deviceFaultRecordMapper.addDeviceFaultRecord(deviceFaultRecord);
        } catch (DuplicateKeyException e) {
            log.warn("ADD [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceFaultRecordDTO, e.toString());
            throw new ResultException(ResultEnum.DUPLICATE_KEY_CONFLICT, "添加设备故障记录失败\n原因: 设备故障记录id已存在");
        } catch (Exception e) {
            log.error("ADD [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceFaultRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "添加设备故障记录失败");
        }
        return status;
    }

    @Override
    public int updateDeviceFaultRecord(DeviceFaultRecordDTO deviceFaultRecordDTO) {
        DeviceFaultRecord deviceFaultRecord = new DeviceFaultRecord();
        BeanUtils.copyProperties(deviceFaultRecordDTO, deviceFaultRecord);

        int status;
        try {
            status = deviceFaultRecordMapper.updateDeviceFaultRecord(deviceFaultRecord);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [deviceFaultRecord] record not exist");
            }
        }catch (DAOException e){
            log.warn("UPDATE [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceFaultRecordDTO, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "更新设备故障记录失败\n原因: 设备故障记录不存在");
        }
        catch (Exception e) {
            log.error("UPDATE [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", deviceFaultRecordDTO, e.toString());
            throw new ResultException(ResultEnum.ERROR, "更新设备故障记录失败");
        }
        return status;
    }

    @Override
    public int deleteDeviceFaultRecordById(Integer id) {
        int status;
        try {
            status = deviceFaultRecordMapper.deleteDeviceFaultRecordById(id);
            if (status == MapperConst.OBJECT_NULL) {
                throw new DAOException("this [deviceFaultRecord] record not exist");
            }
        } catch (DAOException e) {
            log.warn("DELETE [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.RESULT_NULL, "删除设备故障记录失败\n原因: 设备故障记录不存在");
        } catch (Exception e) {
            log.error("DELETE [deviceFaultRecord] FAIL\nINPUT OBJECT: {}\nREASON: {}", id, e.toString());
            throw new ResultException(ResultEnum.ERROR, "删除设备故障记录失败");
        }
        return status;
    }


}



